package algorithm.EditStrMinCost;

/*
给定两个字符串str1和str2，再给定三个整数ic,dc,rc，分别表示插入、删除、替换一个字符的代价，
返回将str1编辑成str2的最小代价
 */
public class EditStrMinCost {
    public static int getCost(String str1, String str2, int ic, int dc, int rc) {
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();
        int[][] dp = new int[s1.length + 1][s2.length + 1];
        for (int i = 1; i < s1.length + 1; i++) {
            dp[i][0] = dc * i;
        }
        for (int i = 1; i < s2.length + 1; i++) {
            dp[0][i] = ic * i;
        }
        return process(s1, s2, dp, ic, dc, rc);
    }

    public static int process(char[] str1, char[] str2, int[][] dp, int ic, int dc, int rc) {
        for (int i = 1; i < str1.length + 1; i++) {
            for (int j = 1; j < str2.length + 1; j++) {
                if (str1[i-1] == str2[j-1]) {
                    dp[i][j] = dp[i-1][j-1];    //copy
                } else {
                    dp[i][j] = dp[i-1][j-1] + rc;   //replace
                }
                dp[i][j] = Math.min(dp[i][j], dp[i-1][j] + dc);    //delete
                dp[i][j] = Math.min(dp[i][j], dp[i][j-1] + ic);    //insert
            }
        }
        return dp[str1.length][str2.length];
    }

    public static void main(String[] args) {
        String str1 = "abcd";
        String str2 = "a1bc1d";
        System.out.println(getCost(str1, str2, 2, 3, 4));
    }
}
